/**  
* @Title: PrinterUtils.java
* @Package com.lemon.utils.printer
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月12日
* @version V1.0  
*/
package com.lemon.utils.printer;

import java.text.DecimalFormat;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lemon.conf.redis.RedisUtil;
import com.lemon.model.cityRun.CrShopOrderGoods;
import com.lemon.model.cityRun.CrShopOrders;
import com.lemon.model.cityRun.CrShopPrinter;
import com.lemon.utils.CityRunUtils;
import com.lemon.utils.EnChValidate;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: PrinterUtils</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月12日
 * @版本 V 1.0 
 */
public class PrinterUtils {

	
	/**
	 * <b>Description:<blockquote> 根据店铺id获取打印机列表, 并自动打印
	 * </blockquote></b>
	 * @Title: printer
	 * @param shopId  店铺id
	 * @return
	 * @throws Exception    
	 * @author Ran_cc
	 * @date 2018年11月14日 
	 */
	public static String printer(List<CrShopPrinter> listPrinters , CrShopOrders orders) throws Exception {
		for (int i = 0; i < listPrinters.size(); i++) {
			CrShopPrinter p = listPrinters.get(i);
			if (p.getType() == 2) {
				String printer = PrinterUtils.printer(orders , p.getDevNo() , p.getDevSecretKey() , 1 , p.getBrand());
			} else if (p.getType() == 3) {
				String string = RedisUtil.get("cityRun:print:print_" + orders.getShopId());
				if (StringUtil.isNotBlank(string)) {
					JSONObject js = (JSONObject) JSONObject.parse(string);
					JSONArray jsonArray = js.getJSONArray("list");
					jsonArray.add(orders);
					js.put("list" , jsonArray);
					RedisUtil.set("cityRun:print:print_" + orders.getShopId() , js);
				} else {
					JSONObject js = new JSONObject();
					JSONArray arr = new JSONArray();
					arr.add(orders);
					js.put("list" , arr);
					RedisUtil.set("cityRun:print:print_" + orders.getShopId() , js);
				}
				//如果是usb 的连接方式 , 将保存最新的cookie时间戳
				CityRunUtils.setOrderTimesCookie(p.getShopId());
			}
		}
		return "bluetooth";
	}
	/**
	 * <b>Description:<blockquote>
	 * </blockquote></b>
	 * 
	 * @Title: printer
	 * @param content   打印内容,是订单的实体类
	 * @param key       打印机编号
	 * @param src       打印机key
	 * @param count     打印次数
	 * @param printType wifi打印机类型 1=365 2=易连云
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	public static String printer(Object content , String key , String src , int count , int printType)
			throws Exception {

		String B = "" , B1 = "" , C = "" , C1 = "" , CB = "" , CB1 = "";
		if (printType == 1) {
			B = "<B>";
			B1 = "</B>";
			C = "<C>";
			C1 = "</C>";
			CB = "<CB>";
			CB1 = "</CB>";
		} else if (printType == 2) {
			B = "<FS2><FB>";
			B1 = "</FB></FS2>";
			C = "<center>";
			C1 = "</center>";
			CB = "<FS2><center>";
			CB1 = "</center></FS2>";
		}

		CrShopOrders o = (CrShopOrders) content;
		List<CrShopOrderGoods> list = o.getList();
		Integer yprice = 0;
		String[] orders = new String[list.size()];
		String[] prices = new String[list.size()];
		String[] sl = new String[list.size()];
		DecimalFormat df = new DecimalFormat("#.00");
		for (int i = 0; i < list.size(); i++) {
			CrShopOrderGoods g = list.get(i);
			orders[i] = g.getGoodsTitle();
			prices[i] = df.format(g.getGoodsPrice() / 100);
			sl[i] = g.getGoodsNum() + "";
			yprice += g.getGoodsNum() * g.getGoodsPrice();
		}
		//		String[] orders = { "西红柿鸡蛋炒饭饭西鸡鸡蛋炒饭饭西鸡鸡蛋炒饭饭西鸡鸡蛋炒饭饭" , "炒饭西红柿鸡蛋炒饭蛋炒饭" , "炒饭西红柿鸡蛋炒蛋炒饭" , "鸡蛋炒饭" , "番茄蛋炒饭" ,
		//				"西红柿蛋炒饭" , "西红柿鸡蛋炒饭" , "西红柿鸡蛋炒冷饭" , "西红柿鸡蛋炒(饭)" , "西红柿鸡蛋狂炒饭" , "西红柿鸡蛋饭hot" };
		//		String[] prices = { "1.09" , "10.09" , "10.09" , "100.09" , "1000.09" , "1000.09" , "100.09" , "100.09" ,
		//				"100.09" , "100.09" , "100.09" };
		//		String[] sl = { "1" , "10" , "10" , "1" , "1" , "1" , "10" , "10" , "100" , "100" , "100" };
		String payStat = "";
		if (o.getStatus() == 1) {
			payStat = "未支付";
		} else {
			payStat = "已支付";
		}
		String hc = "\r\n";
		StringBuffer mess = new StringBuffer("");
		mess.append(CB + "#").append(o.getSort()).append(" 城跑外卖").append(CB1).append("\r\n");
		mess.append("--------------------------------\r\n");
		mess.append("--------------------------------\r\n");
		mess.append(C).append(o.getShopTitle()).append(C1).append("\r\n");
		mess.append(CB).append(o.getPayType() == 1 ? "微信支付" : "现金结算").append("(").append(payStat).append(")")
				.append(CB1).append("\r\n");
		mess.append(C).append("订单号: ").append(o.getOrderNo()).append(C1).append("\r\n");
		mess.append(C).append("下单时间: ").append(StringUtil.DateToString(o.getCreateTime() , 4)).append(C1)
				.append("\r\n");
		if (o.getType() == 2) {
			mess.append("--------------------------------\r\n");
			mess.append(B).append(o.getUserAddress()).append(B1).append("\r\n");
		}
		mess.append("--------------------------------\r\n");
		mess.append(B).append("备注:").append(o.getMark()).append(B1).append("\r\n");
		mess.append("--------------------------------\r\n");

		/*
		 * mess.append("收货人：").append("你妹妹").append(" ").append("18998111555").append(hc);
		 * mess.append("地址：").append("美国省").append("洛杉矶市").append("贫民区").append(hc);
		 * // mess.append("皇后大道东边的北边的西边的南边的后面的前方的1009号三单元999室".substring(0 , 20)).append(hc);
		 * // mess.append("皇后大道东边的北边的西边的南边的后面的前方的1009号三单元999室".substring(20)).append(hc);
		 * mess.append("运费：").append("100.50").append(hc);
		 * // mess.append("扫码来源：").append("hanjingqunale?").append(hc);
		 * mess.append("附言：").append("都给我来最好的啊").append(hc);
		 * mess.append("原价:").append("3000.00").append("总数:").append("13").append("优惠:").append("100.01").append(hc);
		 * mess.append("名称　　　　       数量  单价 \r\n");
		 */
		mess.append("名称　　　　       数量  单价 \r\n");
		mess.append("--------------------------------\r\n");

		for (int i = 0; i < orders.length; i++) {
			int xh = i + 1;
			String ordertitle = orders[i];
			String price = prices[i];
			String ordernum = sl[i];
			String xhstr = xh <= 9 ? xh + ". " : xh + ".";
			mess.append(xhstr);
			mess.append(ordertitle);
			if (EnChValidate.getRegExpLength(ordertitle) > 16) {
				mess.append(hc);
				for (int j = 0; j < 20; j++) {
					mess.append(" ");
				}
			} else {
				for (int j = EnChValidate.getRegExpLength(ordertitle); j < 17; j++) {
					mess.append(" ");
				}
			}
			mess.append("x" + ordernum);
			for (int j = EnChValidate.getRegExpLength(ordernum); j < 4; j++) {
				mess.append(" ");
			}
			mess.append(price);
			mess.append(hc);
		}
		mess.append("--------------------------------\r\n");
		mess.append("包装费:");
		for (int j = 0; j < 19; j++) {
			mess.append(" ");
		}
		mess.append(df.format(o.getPackPrice() / 100)).append("\r\n");

		mess.append("配送费:");
		for (int j = 0; j < 19; j++) {
			mess.append(" ");
		}
		mess.append(df.format(o.getSendPrice() / 100)).append("\r\n");
		int ii = 0;
		//计算活动
		String activityIds = o.getActivityIds();
		if (StringUtil.isNotBlank(activityIds)) {
			String[] actids = activityIds.split(",");
			for (int i = 0; i < actids.length; i++) {
				String[] split = actids[i].split("-");
				switch (split[0]) {
				case "1":
					int pri = Integer.parseInt(split[1]);
					mess.append("新客立减:");
					ii = EnChValidate.getRegExpLength(pri + "");
					for (int j = 0; j < 13 + (6 - ii); j++) {
						mess.append(" ");
					}
					mess.append("-").append(df.format(pri / 100)).append("\r\n");
					break;
				case "2":
					int pri2 = Integer.parseInt(split[1]);
					mess.append("满减活动:");
					ii = EnChValidate.getRegExpLength(pri2 + "");
					for (int j = 0; j < 13 + (6 - ii); j++) {
						mess.append(" ");
					}
					mess.append("-").append(df.format(pri2 / 100)).append("\r\n");
					break;
				case "3":
					int pri3 = Integer.parseInt(split[1]);
					mess.append("单品折扣:");
					ii = EnChValidate.getRegExpLength(pri3 + "");
					for (int j = 0; j < 13 + (6 - ii); j++) {
						mess.append(" ");
					}
					mess.append("-").append(df.format(pri3 / 100)).append("\r\n");
					break;
				case "4":
					int pri4 = Integer.parseInt(split[1]);
					mess.append("满减折扣:");
					ii = EnChValidate.getRegExpLength(pri4 + "");
					for (int j = 0; j < 13 + (6 - ii); j++) {
						mess.append(" ");
					}
					mess.append("-").append(df.format(pri4 / 100)).append("\r\n");
					break;
				case "5":
					int pri5 = Integer.parseInt(split[1]);
					mess.append("满减折扣:");
					ii = EnChValidate.getRegExpLength(pri5 + "");
					for (int j = 0; j < 13 + (6 - ii); j++) {
						mess.append(" ");
					}
					mess.append("-").append(df.format(pri5 / 100)).append("\r\n");
					break;
				default:
					break;
				}
			}

		}

		mess.append("--------------------------------\r\n");

		//		for (int j = EnChValidate.getRegExpLength(yprice+""); j < 4; j++) {
		//			mess.append(" ");
		//		}
		yprice += o.getSendPrice() + o.getPackPrice();
		ii = EnChValidate.getRegExpLength(yprice + "");
		for (int j = 0; j < 18 + (6 - ii); j++) {
			mess.append(" ");
		}
		mess.append("原价:");
		mess.append(df.format(yprice / 100)).append("\r\n");
		//
		ii = EnChValidate.getRegExpLength(o.getPayPrice() + "");
		for (int j = 0; j < 8 + (10 - (ii * 2)); j++) {
			mess.append(" ");
		}
		mess.append(B).append("实付:");
		mess.append(df.format(o.getPayPrice() / 100)).append(B1).append("\r\n");
		if (o.getType() == 1) {
			mess.append("--------------------------------\r\n");
			mess.append(B).append("地址:").append(B1).append("\r\n");
			mess.append(B).append(o.getUserName()).append("\r\n").append(B1);
			mess.append(B).append(o.getUserPhone()).append("\r\n").append(B1);
			mess.append(B).append(o.getUserAddress()).append(B1);
		}
		mess.append("\r\n\n");
		String result = "";
		switch (printType) {
		case 1:
			result = Printer365Utils.print(mess.toString() , key , src , count + "");
			break;
		case 2:
			result = ElinkPrinter.print(mess.toString() , key , src) == true ? "{\"info\":\"Elink Print Succ\"}"
					: "{\"info\":\"Elink Print Err\"}";
			break;
		default:
			break;
		}
		return result;
	}

}
